/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.bean;

import com.yunhoo.base.db.impl.AbroadGroupCurrencyImpl;
import com.yunhoo.base.model.AbroadGroupCurrency;
import com.yunhoo.base.model.MsgModel;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.ToolKit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 *
 * 出国差旅费报销单
 *
 * @author Administrator
 */
@ManagedBean(name ="abroadGroupCurrencyBean")
@SessionScoped
public class AbroadGroupCurrencyBean {

    private AbroadGroupCurrency currency = null;
    private List<AbroadGroupCurrency> groupli = null;
    private DataModel currencyModel = null;
    private boolean editAble;
    private boolean update;

    public AbroadGroupCurrencyBean() {
        currency = new AbroadGroupCurrency();
        groupli = AbroadGroupCurrencyImpl.getCurrencyList();
        currencyModel = new ListDataModel(groupli);
    }

    public void initboolean() {
        editAble = false;
        update = false;
    }

    public void getcurrency() {
        AbroadGroupCurrency temp = (AbroadGroupCurrency) currencyModel.getRowData();
        currency = AbroadGroupCurrencyImpl.getCurrencybyCode(temp.getCode().trim());
        initboolean();
    }

    public void add() {
        currency = new AbroadGroupCurrency();
        editAble = true;
        update = false;
    }

    public void update() {
        editAble = true;
        update = true;
    }

    public void delete() {
        AbroadGroupCurrencyImpl.deleteCurrency(currency.getCode().trim());
        groupli = AbroadGroupCurrencyImpl.getCurrencyList();
        currencyModel.setWrappedData(groupli);
        initboolean();
    }
    public void checkDelete(){
         MsgModel msg = AbroadGroupCurrencyImpl.selectCurrencyMsg(currency.getCode().trim());
        System.out.println("msg:" + msg.getMsg());
        System.out.println("msg:" + msg.getMsg2());
        if (msg.getMsg().equals("") && msg.getMsg2().equals("")) {
            delete();
        } else {
            ToolKit.msgs_err("已在表" + msg.getMsg() + "  " + msg.getMsg2() + "中使用");
        }
    }

    public void cancel() {
        initboolean();
    }

    public void sava() {
        List<AbroadGroupCurrency> li = AbroadGroupCurrencyImpl.checkCurrencyRepeat(currency);
        System.out.println("update : " + update);
        if ("".equals(currency.getCode().trim())) {
            ToolKit.msgs_err("代码不能为空!");
            System.out.println("代码不能为空!");
        } else if ("".equals(currency.getName().trim())) {
            ToolKit.msgs_err("名称不能为空!");
            System.out.println("名称不能为空!");
        } else {
            Map map = new HashMap();
            map.put("code", currency.getCode().trim());
            map.put("name", currency.getName().trim());
            map.put("sign", currency.getSign().trim());
            if (update == false) {
                if (li != null && li.size() > 0) {
                    ToolKit.msgs_err("代码或名称已存在!");
                    System.out.println("代码或名称已存在!");
                } else {
                    System.out.println("插入!!!");
                    AbroadGroupCurrencyImpl.insertCurrency(map);
                    groupli = AbroadGroupCurrencyImpl.getCurrencyList();
                    currencyModel.setWrappedData(groupli);
                }
            }
            if (update == true) {
                AbroadGroupCurrencyImpl.updateCurrency(map);
                    groupli = AbroadGroupCurrencyImpl.getCurrencyList();
                    currencyModel.setWrappedData(groupli);
            }
        }
        initboolean();
    }

    public AbroadGroupCurrency getCurrency() {
        return currency;
    }

    public void setCurrency(AbroadGroupCurrency currency) {
        this.currency = currency;
    }

    public boolean isEditAble() {
        return editAble;
    }

    public void setEditAble(boolean editAble) {
        this.editAble = editAble;
    }

    public DataModel getCurrencyModel() {
        return currencyModel;
    }

    public void setCurrencyModel(DataModel currencyModel) {
        this.currencyModel = currencyModel;
    }


    public List<AbroadGroupCurrency> getGroupli() {
        return groupli;
    }

    public void setGroupli(List<AbroadGroupCurrency> groupli) {
        this.groupli = groupli;
    }

    public boolean isUpdate() {
        return update;
    }

    public void setUpdate(boolean update) {
        this.update = update;
    }

}
    
